<?php
class Message extends CI_Model{
	var  $mid;
	var $author;
	var $content;
	var $public_date;
	
	public function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	public function insertMessage($author, $content){
		$this->author = $author;
		$this->content = $content;
		$this->public_date = time();
		$this->db->insert('message',$this);
	}
	
	public function updateMessage($mid, $author, $content) {
		$this->mid = $mid;
		$this->author = $author;
		$this->content = $content;
		$this->public_date = time();
		$this->db->update('message', $this, array('mid'=>$this->mid));
	}
	
	public function getMessageById($id){
		$this->db->where('mid',$id);
		return $this->db->get('message')->result();
	}
	
	public function getMessage($time){
		$sql = "SELECT * FROM message WHERE public_date >= ? order by public_date asc"; 
		return $this->db->query($sql, array($time))->result_array();
	}
	
	public function getAllMessage(){
		$this->db->order_by('public_date', 'desc');
		$results = $this->db->get('message');
		return $results->result_array();
	}
	
	public function deleteMessage($mid){
		$this->db->where('mid', $mid);
		$this->db->delete('message');
	}
}